<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加页面</title>
</head>
<body>
<form action="/medical/save" method="post">
    <table border="1">
        <tr>
            <td colspan="2">添加核酸检查信息</td>
        </tr>
        <tr>
            <td>被检查人姓名</td>
            <td>
                <input type="text" name="assayUser">
            </td>
        </tr>
        <tr>
            <td>被检测人手机号</td>
            <td>
                <input type="text" name="phone">
            </td>
        </tr>
        <tr>
            <td>被检测人身份证号</td>
            <td>
                <input type="text" name="cardNum">
            </td>
        </tr>
        <tr>
            <td>检测机构</td>
            <td>
                <select name="hospitalId" id="selectHospital">
                    <option value="">-请选择-</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>检测日期</td>
            <td><input type="text" name="assayTime"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit">
                <botton onclick="javascript:window.location.href='/medical/med'">返回</botton>
            </td>
        </tr>
    </table>
    <script src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script th:inline="javascript">
        $(function (){
            var hospitalId=[[${hospitalId}]]
            console.log("ID为:"+hospitalId)
            //请求后台拿到数据-机构的数据
            $.ajax({
                url:'/hospital',
                method:'get',
                dataType:'json',
                success:function (data){
                    let optionStr=" <option value=\"\">-请选择机构-</option>";
                    for (let i = 0; i < data.length; i++) {
                        if (hospitalId==data[i].id){
                            optionStr+=" <option selected value=\""+data[i].id+"\">"+data[i].name+"</option>";
                        }else {
                            optionStr+=" <option value=\""+data[i].id+"\">"+data[i].name+"</option>";
                        }
                    }
                    $("#selectHospital").html(optionStr)
                }
            })
        })
    </script>

</form>
</body>
</html>